package com.sgb.goods.view.activity;

import android.content.Intent;
import android.text.Editable;
import android.text.InputType;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.view.WindowManager;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.PopupWindow;
import android.widget.TextView;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.sdk.android.vod.upload.model.VodInfo;
import com.bigkoo.pickerview.builder.OptionsPickerBuilder;
import com.bigkoo.pickerview.view.OptionsPickerView;
import com.blankj.utilcode.util.ColorUtils;
import com.luck.picture.lib.PictureSelector;
import com.luck.picture.lib.config.PictureConfig;
import com.luck.picture.lib.entity.LocalMedia;
import com.sgb.goods.BR;
import com.sgb.goods.R;
import com.sgb.goods.databinding.GoodsActivityGoodsPublishBinding;
import com.sgb.goods.entity.ArrEntity;
import com.sgb.goods.entity.FilterEntity;
import com.sgb.goods.entity.GoodsPublishEntity;
import com.sgb.goods.entity.GoodsPublishInfoEntity;
import com.sgb.goods.entity.GoodsPublishSkuListEntity;
import com.sgb.goods.entity.GoodsServicesEntity;
import com.sgb.goods.entity.MenuEntity1;
import com.sgb.goods.entity.PublicTypeEntity;
import com.sgb.goods.entity.STArrEntity;
import com.sgb.goods.entity.STPublishEntity;
import com.sgb.goods.entity.StorageManEntity;
import com.sgb.goods.entity.StrengthEntity;
import com.sgb.goods.entity.VideoChangeEntity;
import com.sgb.goods.entity.VideoEntity;
import com.sgb.goods.utils.Constants;
import com.sgb.goods.utils.SmartUtils;
import com.sgb.goods.utils.base.BaseActivity;
import com.sgb.goods.view.adapter.publish.UtilSelectUtils;
import com.sgb.goods.view.adapter.publish.VideoAdapter;
import com.sgb.goods.view.adapter.publish.pop.VideoSelectPopWindow;
import com.sgb.goods.viewmodel.publish.DynamicComponentsViewModel;
import com.sgb.goods.viewmodel.publish.GoodsPublishViewModel;
import com.sgb.goods.viewmodel.publish.ImageUploadViewModel;
import com.sgb.goods.viewmodel.publish.PublishPopViewModel;
import com.sgb.goods.viewmodel.publish.VideoUploadViewModel;
import com.swgk.core.customview.ItemInputTextView;
import com.swgk.core.dialog.RoundCornerDialog;
import com.swgk.core.util.MLog;
import com.swgk.core.util.MToast;
import com.swgk.core.util.SharedPreferenceUtil;
import com.swgk.core.view.CityPickBuilder;
import com.swgk.core.view.CityPickView;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.File;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

import fm.jiecao.jcvideoplayer_lib.JCVideoPlayer;
import fm.jiecao.jcvideoplayer_lib.JCVideoPlayerStandard;

/**
 * 商品发布 | 商砼发布 | 编辑商品
 * @author cyj
 */
@Route(path = Constants.ARouter.AR_GOODS_PUBLISH)
public class GoodsPublishActivity extends BaseActivity<GoodsActivityGoodsPublishBinding, GoodsPublishViewModel> implements ItemInputTextView.OnWholeItemClickListener {

    public static final int VIDEO_ADAPTER_LIST = 119110;
    public static final int IMAGE_ADAPTER_LIST = 110119;

    public boolean isVideoS;
    public List<String> editUrlsList = new ArrayList<>();
    public List<LocalMedia> adapterLocVideoList = new ArrayList<>();
    private Map<Integer, List<LocalMedia>> mPVideoSelectList = new HashMap();
    private int arrPosition;

    private List<PublicTypeEntity<List<PublicTypeEntity>>> publicTypeEntityList;
    private List<FilterEntity<List<FilterEntity>>> attrList;
    //属性子列表
    private List<ArrEntity.ChildType> childTypeList;
    //强度等级和外加剂
    private List<STArrEntity> strength;
    private Map map = new HashMap();
    //视频列表弹窗
    private VideoSelectPopWindow videoListSelectPopWindow;
    //品牌搜索内容
    private String brandSearch = "";
    //单位搜索内容
    private String unitSearch = "";
    //商品分类ID
    private String cId3 = "";
    //商品分类名称
    private String cName3 = "";

    //填写了的强度等级
    private List<StrengthEntity> strengthEntityList;

    //填写了的外加剂
    private List<StrengthEntity> wjjList;
    //属性是否都已经选择
    private boolean isSelect = false;
    //商品分类是否再次选择
    private boolean typeSelect = false;
    //详情id
    private String id;

    //没选择的属性名称
    private String arrName;
    //被选中城市的下标
    private int cityPosition;
    //是否是商砼
    private boolean isST;
    private List<GoodsPublishSkuListEntity> combination;
    //cNmae3是否发生变化
    private boolean cName3IsChange = false;
    // 视频List
    private List<VideoEntity> listVideo;
    private boolean IS_START_PUBLISH = true;
    /** 操作类型。1：发布商品；2：编辑商品； */
    private String operationType = "1";

    private boolean isSelectDay = true, isSelectLeft;//用于回显期货弹框数据：是否选择天，是否是现货
    private String dataText = "";//显示时间
    private boolean islooks;

    private ImageUploadViewModel imageUploadViewModel;
    private DynamicComponentsViewModel dcpViewModel;
    private VideoUploadViewModel videoUploadViewModel;
    private PublishPopViewModel publishPopViewModel;

    @Override
    protected int getLayoutResId() {
        return R.layout.goods_activity_goods_publish;
    }

    @Override
    protected void initViewModel() {
        mViewModel = createViewModel(GoodsPublishViewModel.class, BR.viewModel);
        imageUploadViewModel = createViewModel(ImageUploadViewModel.class);
        dcpViewModel = createViewModel(DynamicComponentsViewModel.class);
        videoUploadViewModel = createViewModel(VideoUploadViewModel.class);
        publishPopViewModel = createViewModel(PublishPopViewModel.class);
    }

    @Override
    protected void init() {
        mDataBinding.head.title.setText("商品发布");
        id = getIntent().getStringExtra("id");
        operationType = getIntent().getStringExtra("operationType");
        isST = getIntent().getBooleanExtra("isST", false);

        mDataBinding.itemBuyLow.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL);
        mDataBinding.itemTvBsprice.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL);
        mDataBinding.itemTvBsprice.setCheck(4, 2);
        mDataBinding.itemTvLeavePrice.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL);
        mDataBinding.itemTvTax.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL);
        mDataBinding.itemTvTax.setNumberAndLetter("0123456789");
        mDataBinding.itemTvTax.setCheck(2, 2);
        mDataBinding.itemTvPrice.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL);
        mDataBinding.itemTvLeavePrice.setCheck(4, 2);

        mDataBinding.recyclerView.setLayoutManager(new GridLayoutManager(GoodsPublishActivity.this, 4));
        mDataBinding.recyclerView.setAdapter(imageUploadViewModel.imageAdapter);

        mDataBinding.recyclerViewVideo.setLayoutManager(new GridLayoutManager(GoodsPublishActivity.this, 4));
        mDataBinding.recyclerViewVideo.setAdapter(videoUploadViewModel.videoAdapter);

        // 初始化视频相关 模块
        initVideoModule();

        initView();
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);

        // 获取数据
        if (id != null) {
            IS_START_PUBLISH = false;
            if (!isST) {
                mViewModel.getNewGoodsById(id);
                dcpViewModel.getGoodsOtherInfo(id);
            } else {
                mViewModel.concretePublishDetail(id);
                dcpViewModel.getStOtherInfo(id);
            }
        }
    }

    /** 初始化视频相关 模块 */
    private void initVideoModule() {
        //-------------------------视频列表选择弹窗----------------------------------------------
        if (videoListSelectPopWindow == null) {
            videoListSelectPopWindow = new VideoSelectPopWindow(GoodsPublishActivity.this);
        }
        videoListSelectPopWindow.setBgView(mDataBinding.grayLayout);
        videoListSelectPopWindow.setInputMethodMode(PopupWindow.INPUT_METHOD_NEEDED);
        videoListSelectPopWindow.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
        videoListSelectPopWindow.setOnResultClick((key1, key2, key3) -> {
            isVideoS = true;
            selectVideo2((int) key1, (VideoEntity) key2, (int) key3);
        });
    }

    @Override
    protected void initListener() {
        mDataBinding.itemTvArr.setOnWholeItemClickListener(this);
        mDataBinding.itemTvStorage.setOnWholeItemClickListener(this);
        mDataBinding.itemTvBrand.setOnWholeItemClickListener(this);
        mDataBinding.itemTvUnit.setOnWholeItemClickListener(this);
        mDataBinding.itemTvFutures.setOnWholeItemClickListener(this);
        mDataBinding.head.ivBack.setOnClickListener(v -> {
            if(!IS_START_PUBLISH) {
                finish();
            } else {
                showSaveDialog();
            }
        });
        mDataBinding.nextView.setOnClickListener(v -> {
            //商品直接发布不保存草稿
            IS_START_PUBLISH = false;
            if (isST) {
                stCheck();//商砼验证
            } else {
                goodsCheck();//商品验证
            }
        });
        mDataBinding.tvTitleClick.setOnClickListener(v -> {
            if ("".equals(mDataBinding.etName.getText().toString().trim())) {
                MToast.showToast(GoodsPublishActivity.this, "请填写完整名称、分类后预览");
                return;
            } else if ("".equals(mDataBinding.itemTvArr.getContent())) {
                MToast.showToast(GoodsPublishActivity.this, "请填写完整名称、分类后预览");
                return;
            }
            if (TextUtils.isEmpty(mViewModel.goodsPublishEntity.get().brandName)) {
                mDataBinding.etTitleLook.setText(mViewModel.goodsPublishEntity.get().name + "/" + mViewModel.goodsPublishEntity.get().cid3Name);
            } else {
                mDataBinding.etTitleLook.setText(mViewModel.goodsPublishEntity.get().name + "/" +
                        mViewModel.goodsPublishEntity.get().brandName + "/" + mViewModel.goodsPublishEntity.get().cid3Name);
            }
        });
        mDataBinding.deleteTv.setOnClickListener(v -> {
            //停止播放
            mDataBinding.videoView.release();
            mDataBinding.rlVideo.setVisibility(View.GONE);
        });
        imageUploadViewModel.imageAdapter.OnSelectPop(this::showSelectAlbumPopWindow);
        mViewModel.arrAdapter.OnClickArr((childType, index, pName, pId) -> showArrPop(childType, index));
        mDataBinding.etName.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {}

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (s.toString().contains(" ")) {
                    String[] str = s.toString().split(" ");
                    StringBuffer sb = new StringBuffer();
                    for (int i = 0; i < str.length; i++) {
                        sb.append(str[i]);
                    }
                    mDataBinding.etName.setText(sb.toString());
                    mDataBinding.etName.setSelection(start);
                }
                if (!isST) {
                    if (TextUtils.isEmpty(mViewModel.goodsPublishEntity.get().cid3Name)) {
                        mDataBinding.etTitleLook.setText(s);
                    } else {
                        if (TextUtils.isEmpty(mViewModel.goodsPublishEntity.get().brandName)) {
                            mDataBinding.etTitleLook.setText(s + "/" + mViewModel.goodsPublishEntity.get().cid3Name);
                        } else {
                            mDataBinding.etTitleLook.setText(s + "/" +
                                    mViewModel.goodsPublishEntity.get().brandName + "/" + mViewModel.goodsPublishEntity.get().cid3Name);
                        }
                    }
                }
            }

            @Override
            public void afterTextChanged(Editable s) {}
        });
        videoUploadViewModel.videoAdapter.OnSelectPop(new VideoAdapter.ISelectPop() {

            @Override
            public void openPop(int index) {
                videoUploadViewModel.openPopWindow(mDataBinding.llPar, index);
            }

            @Override
            public void openVideo(VideoEntity data) {
                mDataBinding.videoView.setUp(data.url, JCVideoPlayerStandard.SCREEN_LAYOUT_NORMAL, data.title);
                mDataBinding.rlVideo.setVisibility(View.VISIBLE);
            }
        });
    }

    @Override
    protected void initObserve() {
        mViewModel.goodsPublishEntityData.observe(this, this::setGoodsDetailParams);
        mViewModel.sTPublishLD.observe(this, this::setStDetailParams);
        imageUploadViewModel.uploadImgUrlLD.observe(this, this::uploadImgSuccess);
        mViewModel.goodsServiceListLD.observe(this, data -> {
            mDataBinding.tvChip.setVisibility(data.size() > 0 ? View.VISIBLE : View.GONE);
            mDataBinding.tvChip.setLabelNameColor(ColorUtils.getColor(R.color.goods_color_FF333333));
            if (data.size() > 0) {
                mDataBinding.tvChip.setContent("");
            }
        });
        dcpViewModel.addLayoutLD.observe(this, list -> {
            // 动态创建View布局
            dcpViewModel.addMoveLayout(mDataBinding, list);
        });
        dcpViewModel.showVideoListSelectPopLD.observe(this, dataRest -> {
            // 重置弹窗数据
            videoListSelectPopWindow.setDataRest(dataRest);
            videoListSelectPopWindow.showDownPopwindow(mDataBinding.grayLayout, true);
        });
//        mViewModel.albumListLD.observe(this, list -> {
//            //selectAlbumPopWindow.setData(list);
//            cName3IsChange = false;
//            showSelectAlbumPopWindow123();
//        });
        mViewModel.isErrorLD.observe(this, b -> {
            // 重置上传属性
            imageUploadViewModel.uploadImgUrlList.clear();
            imageUploadViewModel.currentImgNum = 0;
            mDataBinding.nextView.setClickable(true);
        });
        mViewModel.isSuccessLD.observe(this, b -> {
            MToast.showToast(GoodsPublishActivity.this, "发布成功!");
            mDataBinding.nextView.setClickable(false);
            // fixme：应该是跳转商品、商砼详情
            // EventBus.getDefault().postSticky(GoodsEvent.make().setMessageTag(EVENT_GOODS_PUBLISH));
            if (id == null) {
                // fixme: 跳转商品管理页面
                // GoodsListActivity3.start(this);
            }
            finish();
        });
        mViewModel.arrListLD.observe(this, list -> {
            if (list != null && list.size() != 0) {
                mDataBinding.recyclerArr.setVisibility(View.VISIBLE);
                mDataBinding.itemTvFutures.setDeliverLineVG(false);
            } else {
                mDataBinding.itemTvFutures.setDeliverLineVG(true);
                islooks = false;
                mDataBinding.recyclerArr.setVisibility(View.GONE);
            }
        });
        mViewModel.provinceListLD.observe(this, list -> showCityPop());
        mViewModel.getSkuListLD.observe(this, list -> {
            mDataBinding.llSpec.setVisibility(null != list && list.size() != 0 ? View.VISIBLE : View.GONE);
            mDataBinding.itemTvPrice.setVisibility(null != list && list.size() != 0 ? View.GONE : View.VISIBLE);
            islooks = null != list && list.size() <= 0;
        });
    }

    /** 设置商砼详情相关属性 */
    private void setStDetailParams(STPublishEntity entity) {
        setGoneAndDisplay();
        mDataBinding.itemTvArea.setContent(entity.sellProvinceName);
        mDataBinding.itemTvArr.setContent("商砼-混凝土-混凝土");
        cName3 = "混凝土";
        mDataBinding.etName.setText(entity.title);
        mDataBinding.etMark.setText(entity.itemDesc);
        mDataBinding.itemTvStorage.setContent(entity.depotName);
        mDataBinding.itemTvTax.setContent(entity.tax);
        mViewModel.concreteAttrs(1);
        mDataBinding.itemTvFutures.setVisibility(View.GONE);
        mDataBinding.itemBuyLow.setVisibility(View.GONE);
        if (entity.itemServerAttrVals != null && entity.itemServerAttrVals.size() != 0) {
            mViewModel.listService = entity.itemServerAttrVals;
            if (isHasChecked(mViewModel.listService)) {
                mDataBinding.tvChip.setContent("已填写");
            } else {
                mDataBinding.tvChip.setContent("");
            }
            mDataBinding.tvChip.setVisibility(View.VISIBLE);
        } else {
            mDataBinding.tvChip.setVisibility(View.GONE);
        }
        //设置视频
        List<VideoEntity> videoEntities = new ArrayList<>();
        if (entity.videoList != null) {
            for (int i = 0; i < entity.videoList.size(); i++) {
                VideoEntity videoEntity = new VideoEntity();
                //视频类型
                videoEntity.type = entity.videoList.get(i).type;
                //视频id
                videoEntity.videoId = entity.videoList.get(i).videoId;
                //视频名称
                videoEntity.title = entity.videoList.get(i).videoName;
                //视频地址
                videoEntity.url = entity.videoList.get(i).playUrl;
                //视频时长
                videoEntity.time = entity.videoList.get(i).time;
                videoEntities.add(videoEntity);
            }
        }
        videoUploadViewModel.videoAdapter.addData(videoEntities);
        imageUploadViewModel.imageAdapter.setNetImgList(entity.imageUrls);
        editUrlsList.addAll(entity.imageUrls);
        if (entity.itemDetail != null && !"".equals(entity.itemDetail)) {
            setHtml(entity.itemDetail);
        }
    }

    /** 设置商品详情相关属性 */
    private void setGoodsDetailParams(GoodsPublishEntity entity) {
        setGoneAndDisplay();
        cName3 = entity.cid3Name;
        mDataBinding.itemTvArr.setContent(entity.cid1Name + "-" + entity.cid2Name + "-" + entity.cid3Name);
        imageUploadViewModel.imageAdapter.setNetImgList(entity.imageUrls);
        mViewModel.attrChildEntityList = entity.attrList;
        // 获取属性
        mViewModel.getMaterChoose(entity.cid3);
        mDataBinding.itemTvFutures.setVisibility(View.VISIBLE);
        mDataBinding.itemBuyLow.setVisibility(View.VISIBLE);
        setBuyLow(entity.unit, false);
        mDataBinding.itemBuyLow.setContent(entity.lowestBuy);

        if ((entity.commodityType == 1)) {
            mDataBinding.itemTvFutures.setContent("现货");
            isSelectLeft = true;
        } else if (entity.commodityType == 2) {
            //货期时间 单位 1：天 2：月
            isSelectLeft = false;
            String s = entity.deliveryTimeUnit == 1 ? entity.deliveryTime + "天" : entity.deliveryTime + "月";
            mDataBinding.itemTvFutures.setContent(s);
            dataText = entity.deliveryTime + "";
            isSelectDay = entity.deliveryTimeUnit == 1;
        } else {
            mDataBinding.itemTvFutures.setContent("");
        }
        //设置视频
        List<VideoEntity> videoEntities = new ArrayList<>();
        if (entity.videoList != null)
            for (int i = 0; i < entity.videoList.size(); i++) {
                VideoEntity videoEntity = new VideoEntity();
                //视频类型
                videoEntity.type = entity.videoList.get(i).type;
                //视频id
                videoEntity.videoId = entity.videoList.get(i).videoId;
                //视频名称
                videoEntity.title = entity.videoList.get(i).videoName;
                //视频地址
                videoEntity.url = entity.videoList.get(i).playUrl;
                //视频时长
                videoEntity.time = entity.videoList.get(i).time;
                videoEntities.add(videoEntity);
            }
        videoUploadViewModel.videoAdapter.addData(videoEntities);
        cId3 = entity.cid3;
        mDataBinding.itemTvArea.setContent(entity.sellProvinceName);
        editUrlsList.addAll(entity.imageUrls);
        if (entity.itemDetail != null && !entity.itemDetail.equals("")) {
            setHtml(entity.itemDetail);
        }
        mViewModel.getSku(entity.cid3);
        if (entity.skuList != null && entity.skuList.size() != 0) {
            mDataBinding.itemTvSpec.setContent("已填写");
        }
        if (entity.itemServerAttrVals != null && entity.itemServerAttrVals.size() != 0) {
            mViewModel.listService = entity.itemServerAttrVals;
            mDataBinding.tvChip.setContent(isHasChecked(mViewModel.listService) ? "已填写" : "");
            mDataBinding.tvChip.setVisibility(View.VISIBLE);
        } else {
            mDataBinding.tvChip.setVisibility(View.GONE);
        }
        combination = entity.skuList;
        entity.skuOldList = combination;
    }

    private void setGoneAndDisplay() {
        islooks = false;
        if (!isST) {
            mDataBinding.itemTvBrand.setVisibility(View.VISIBLE);
            mDataBinding.itemTvUnit.setVisibility(View.VISIBLE);
            if(null != mViewModel.goodsPublishEntity.get().skuList) {
                mDataBinding.itemTvPrice.setVisibility(mViewModel.goodsPublishEntity.get().skuList.size() != 0 ? View.VISIBLE : View.GONE);
                islooks = mViewModel.goodsPublishEntity.get().skuList.size() != 0;
            }
            mDataBinding.llPreview.setVisibility(View.VISIBLE);
            mDataBinding.itemTvBsprice.setVisibility(View.GONE);
            mDataBinding.itemTvLeavePrice.setVisibility(View.GONE);
            mDataBinding.itemTvStunit.setVisibility(View.GONE);
            mDataBinding.llSt.setVisibility(View.GONE);

        } else {
            mDataBinding.itemTvBrand.setVisibility(View.GONE);
            mDataBinding.itemTvUnit.setVisibility(View.GONE);
            mDataBinding.itemTvPrice.setVisibility(View.GONE);
            mDataBinding.recyclerArr.setVisibility(View.GONE);
            mDataBinding.llPreview.setVisibility(View.GONE);
            mDataBinding.itemTvBsprice.setVisibility(View.VISIBLE);
            mDataBinding.itemTvLeavePrice.setVisibility(View.VISIBLE);
            mDataBinding.itemTvStunit.setVisibility(View.VISIBLE);
            mDataBinding.llSt.setVisibility(View.VISIBLE);

        }
    }

    /** 文件上传成功后进行商品、商砼发布 */
    public void uploadImgSuccess(List<String> urlList) {
        GoodsPublishEntity goodsEntity = mViewModel.goodsPublishEntity.get();
        STPublishEntity stEntity = mViewModel.sTPublishEntity.get();
        // 编辑商品
        if("2".equals(operationType)) {
            urlList.clear();
            editUrlsList.clear();
            // fixme: 这里要存放上传图片的地址
            //editUrlsList.addAll(imageUploadViewModel.imageAdapter.getNetUrls());
            // 商品/商砼
            if(isST) {
                if((goodsEntity.imageUrls != null)) {
                    goodsEntity.imageUrls.clear();
                    goodsEntity.imageUrls.addAll(editUrlsList);
                    goodsEntity.imageUrls.addAll(urlList);
                    urlList.addAll(goodsEntity.imageUrls);
                }
            } else {
                if((stEntity.imageUrls != null)) {
                    stEntity.imageUrls.clear();
                    stEntity.imageUrls.addAll(editUrlsList);
                    stEntity.imageUrls.addAll(urlList);
                    urlList.addAll(stEntity.imageUrls);
                }
            }
        }

        List<VideoChangeEntity> list = videoUploadViewModel.getVideoChangeEntityList();
        if(isST) {
            stEntity.draft = IS_START_PUBLISH ? 5 : 1;
            stEntity.videoList = list;
            stEntity.imageUrls = urlList;
            if (list.size() > 0) {
                stEntity.videoList.get(0).type = 1;
            }
            mViewModel.concretePublish();
        } else {
            //商品
            goodsEntity.status = IS_START_PUBLISH ? 5 : 1;
            goodsEntity.videoList = list;
            goodsEntity.imageUrls = urlList;
            if (list != null && list.size() > 0) {
                goodsEntity.videoList.get(0).type = 1;
            }
            mViewModel.goodsPublish();
        }
    }

    /** 选择相册弹窗 */
    private void showSelectAlbumPopWindow() {
        if(null == publishPopViewModel.selectAlbumPopWindow) {
            publishPopViewModel.getSelectAlbumPopWindow(mDataBinding.llPar).setOnResultClick((key1, key2, key3) -> {
                int type = (int) key1;
                if (type == 0) {
                    imageUploadViewModel.imageAdapter.openAlbum();
                } else {
                    MToast.showToast(GoodsPublishActivity.this, "暂未实现商砼的网络相册选择");
                    // fixme: 这应该是跳转选择图库的页面的
                    //EventBus.getDefault().postSticky(MaterialEvent.make().setMessageTag(EventConstant.EVENT_GOODS_NET_IMG_SELECT_RE).setMessageData(netUrl).setObject(canSelect));
                }
            });
        }
        publishPopViewModel.getSelectAlbumPopWindow(mDataBinding.llPar).showAsDropDown(mDataBinding.llPar);
    }

    /** 选择贷期 */
    private void showFuturesPopWindow() {
        if(null == publishPopViewModel.futuresPopWindow) {
            publishPopViewModel.getFuturesPopWindow(mDataBinding.llPar).setOnResultClick((key1, key2, key3) -> {
                if (TextUtils.isEmpty(key1.toString())) {
                    mDataBinding.itemTvFutures.setContent("");
                    mViewModel.goodsPublishEntity.get().commodityType = 0;
                    mViewModel.goodsPublishEntity.get().deliveryTimeUnit = 0;
                } else {
                    if ((boolean) key2) {
                        mDataBinding.itemTvFutures.setContent(key1.toString());
                        mViewModel.goodsPublishEntity.get().commodityType = 1;
                        isSelectLeft = true;
                    } else {
                        isSelectLeft = false;
                        dataText = key1.toString();
                        mViewModel.goodsPublishEntity.get().commodityType = 2;
                        mViewModel.goodsPublishEntity.get().deliveryTime = Long.valueOf(key1.toString());
                        if ((boolean) key3) {
                            isSelectDay = true;
                            mDataBinding.itemTvFutures.setContent(key1.toString() + "日");
                            mViewModel.goodsPublishEntity.get().deliveryTimeUnit = 1;
                        } else {
                            isSelectDay = false;
                            mViewModel.goodsPublishEntity.get().deliveryTimeUnit = 2;
                            mDataBinding.itemTvFutures.setContent(key1.toString() + "个月");
                        }
                    }
                }
            });
        }
        publishPopViewModel.getFuturesPopWindow(mDataBinding.llPar).setPopData(dataText, isSelectLeft, isSelectDay);
        publishPopViewModel.getFuturesPopWindow(mDataBinding.llPar).showAsDropDown(mDataBinding.llPar, 0, SmartUtils.getBarHeightPx());
    }

    protected void initView() {

        // 商品类型弹窗点击确认按钮监听
        publishPopViewModel.getGoodsTypePopWindow().setOnResultClick((key1, key2, key3) -> {
            // 一级（左侧列表一级数据），obj1.id 代表 cid1
            MenuEntity1 obj1 = (MenuEntity1) key1;
            // 二级（右侧列表一级数据），obj2.id 代表 cid2
            MenuEntity1 obj2 = (MenuEntity1) key2;
            // 三级（右侧列表二级数据），obj3.id 代表 cid3
            MenuEntity1 obj3 = (MenuEntity1) key3;

            if (!"商砼".equals(obj1.name)) {
                mDataBinding.itemTvFutures.setVisibility(View.VISIBLE);
                mDataBinding.itemBuyLow.setVisibility(View.VISIBLE);
                isST = false;
                if (!cId3.equals(obj3.id)) {
                    typeSelect = true;
                    mViewModel.getMaterChoose(obj3.id);
                    mViewModel.getServiceCategory(obj3.id, false);
                    mViewModel.isReset = true;
                    mDataBinding.itemTvBrand.setContent("");
                    mViewModel.goodsPublishEntity.get().brandId = null;
                    mViewModel.getSku(obj3.id);
                    dcpViewModel.getAddLayout(obj3.id);
                    cName3IsChange = true;
                }
                if (!TextUtils.isEmpty(mViewModel.goodsPublishEntity.get().name)) {
                    if (TextUtils.isEmpty(mViewModel.goodsPublishEntity.get().brandName)) {
                        mDataBinding.etTitleLook.setText(mViewModel.goodsPublishEntity.get().name + "/" + obj3.name);
                    } else {
                        mDataBinding.etTitleLook.setText(mViewModel.goodsPublishEntity.get().name + "/" +
                                mViewModel.goodsPublishEntity.get().brandName + "/" + obj3.name);
                    }
                }
            } else {
                if (!cId3.equals(obj3.id)) {
                    dcpViewModel.getAddLayout(obj3.id);
                }
                isST = true;
                mViewModel.concreteAttrs(0);
                mViewModel.getServiceCategory(obj3.id, true);
                mDataBinding.itemTvFutures.setVisibility(View.GONE);
                mDataBinding.itemBuyLow.setVisibility(View.GONE);
                mDataBinding.llSpec.setVisibility(View.GONE);
            }
            cId3 = obj3.id;
            cName3 = obj3.name;
            mDataBinding.itemTvArr.setContent(obj1.name + "-" + obj2.name + "-" + obj3.name);
            mViewModel.goodsPublishEntity.get().cid1 = obj1.id;
            mViewModel.goodsPublishEntity.get().cid2 = obj2.id;
            mViewModel.goodsPublishEntity.get().cid3 = obj3.id;
            mViewModel.goodsPublishEntity.get().cid1Name = obj1.name;
            mViewModel.goodsPublishEntity.get().cid2Name = obj2.name;
            mViewModel.goodsPublishEntity.get().cid3Name = obj3.name;
            setGoneAndDisplay();
        });
        // 仓储弹窗点击确认按钮监听
        publishPopViewModel.getStoragePopWindow(mDataBinding.llPar).setOnResultClick((key1, key2, key3) -> {
            StorageManEntity storageManEntity = (StorageManEntity) key1;
            if (storageManEntity == null)
                return;
            if(!isST) {
                //商品
                mDataBinding.itemTvStorage.setContent(storageManEntity.storageName);
                mViewModel.goodsPublishEntity.get().depotNo = storageManEntity.no;
                mViewModel.goodsPublishEntity.get().provinceNo = storageManEntity.provinceNo;
                mViewModel.goodsPublishEntity.get().cityNo = storageManEntity.cityNo;
                mViewModel.goodsPublishEntity.get().districtNo = storageManEntity.districtNo;
            } else {
                //商砼
                mViewModel.sTPublishEntity.get().depotNo = storageManEntity.no;
                mViewModel.sTPublishEntity.get().provinceNo = storageManEntity.provinceNo;
                mViewModel.sTPublishEntity.get().cityNo = storageManEntity.cityNo;
                mViewModel.sTPublishEntity.get().districtNo = storageManEntity.districtNo;
            }
        });
        // 品牌弹窗点击确认按钮监听
        publishPopViewModel.getBrandPopWindow(mDataBinding.llPar).setOnResultClick((key1, key2, key3) -> {
            mDataBinding.itemTvBrand.setContent((String) key2);
            mViewModel.goodsPublishEntity.get().brandId = (String) key1;
            if (!TextUtils.isEmpty(mViewModel.goodsPublishEntity.get().name)) {
                mDataBinding.etTitleLook.setText(mViewModel.goodsPublishEntity.get().name + "/" +
                        mViewModel.goodsPublishEntity.get().brandName + "/" + mViewModel.goodsPublishEntity.get().cid3Name);
            }
        });
        // 单位弹窗点击确认按钮监听
        publishPopViewModel.getUnitPopWindow(mDataBinding.llPar).setOnResultClick((key1, key2, key3) -> {
            mDataBinding.itemBuyLow.setContent("");
            setBuyLow((String) key2, true);
            mDataBinding.itemTvUnit.setContent((String) key2);
        });
    }

    private void setBuyLow(String key, boolean isChange) {
        if (UtilSelectUtils.isDouble3(key)) {
            mDataBinding.itemBuyLow.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL);
            mDataBinding.itemBuyLow.setCheck(6, 3, true);
            if (isChange) {
                mDataBinding.itemBuyLow.setContent("1.000");
            }
        } else {
            mDataBinding.itemBuyLow.setInputType(InputType.TYPE_CLASS_NUMBER);
            mDataBinding.itemBuyLow.setEditLength(6);
            if (isChange) {
                mDataBinding.itemBuyLow.setContent("1");
            }
        }
    }

    /** 视频列表选择 */
    public void selectVideo2(int status, VideoEntity videoEntity, int indexVideo) {
        dcpViewModel.mPVideoIndex.put(dcpViewModel.mVideoAdapterPosition, indexVideo);
        if (status == 1) {
            dcpViewModel.mPVideoAdapter.get(dcpViewModel.mVideoAdapterPosition).addData(videoEntity);
            adapterLocVideoList.addAll(mPVideoSelectList.get(dcpViewModel.mVideoAdapterPosition));
            for (LocalMedia localMedia : adapterLocVideoList) {
                File file;
                if (localMedia.getRealPath() == null) {
                    file = new File(localMedia.getPath());
                } else {
                    file = new File(localMedia.getRealPath());
                }
                dcpViewModel.mPVideoFile.get(dcpViewModel.mVideoAdapterPosition).add(file);
            }
            addSomeFileToAli(videoEntity.title);
            mViewModel.getVideoParams();
        }
    }

    /** 添加多个文件 */
    private void addSomeFileToAli(String videoName) {
        VodInfo vodInfo = new VodInfo();
        vodInfo.setTitle(videoName);
        vodInfo.setDesc("自定义视频");
        vodInfo.setCateId((int) System.currentTimeMillis());
        vodInfo.setTags(null);
//        if (mPVideoSelectList.get(dcpViewModel.mVideoAdapterPosition).get(0).getRealPath() != null) {
//            uploader.addFile(mPVideoSelectList.get(dcpViewModel.mVideoAdapterPosition).get(0).getRealPath(), vodInfo);
//        } else {
//            uploader.addFile(mPVideoSelectList.get(dcpViewModel.mVideoAdapterPosition).get(0).getPath(), vodInfo);
//        }
    }

    //出售地区弹窗
    public void showCityPop() {
        //条件选择器
        CityPickView cityOption = new CityPickBuilder(GoodsPublishActivity.this, (options1, options2, options3, v) -> {
            String content = mViewModel.provinceListLD.getValue().get(options1).areaName;
            mDataBinding.itemTvArea.setContent(content);
            cityPosition = options1;
            //设置商砼发布的出售地区
            mViewModel.sTPublishEntity.get().sellProvinceNo = mViewModel.provinceListLD.getValue().get(options1).id + "";
            mViewModel.sTPublishEntity.get().sellProvinceName = content;
            //设置商品发布的出售地区
            mViewModel.goodsPublishEntity.get().sellProvinceNo = mViewModel.provinceListLD.getValue().get(options1).id + "";
        }).build();
        cityOption.setPicker(mViewModel.provinceListLD.getValue());
        cityOption.setSelectOptions(cityPosition);
        cityOption.setTitleText("出售地区");
        cityOption.show();
    }

    public void showArrPop(List<ArrEntity.ChildType> childTypeList, int index) {
        this.childTypeList = childTypeList;
        showArrPop(index);
    }

    private void showArrPop(int index) {
        OptionsPickerView businessOption = new OptionsPickerBuilder(GoodsPublishActivity.this, (options1, options2, options3, v) -> {
            String content = childTypeList.get(options1).cname;
            mViewModel.arrAdapter.updateStatus(index, content);
            mViewModel.attrChildEntityList.get(index).attrValue = childTypeList.get(options1).cname;
            mViewModel.attrChildEntityList.get(index).attrValueId = childTypeList.get(options1).cid;
            map.put(index, options1);
        }).build();
        if (map.get(index) == null) {
            businessOption.setSelectOptions(arrPosition);
        } else {
            businessOption.setSelectOptions((Integer) map.get(index));
        }
        businessOption.setPicker(childTypeList);
        businessOption.setTitleText("选择属性值");
        businessOption.show();
    }

    private void showSaveDialog() {
        View view = View.inflate(this, R.layout.dialog_two_btn, null);
        final RoundCornerDialog roundCornerDialog = new RoundCornerDialog(this, 0, 0, view, R.style.RoundCornerDialog);
        roundCornerDialog.show();
        roundCornerDialog.setCanceledOnTouchOutside(false);// 设置点击屏幕Dialog不消失

        TextView tv_confirm = (TextView) view.findViewById(R.id.tv_logout_confirm);
        TextView tv_message = (TextView) view.findViewById(R.id.tv_message);
        TextView tv_title = view.findViewById(R.id.tv_title);
        TextView tv_logout_confirm = (TextView) view.findViewById(R.id.tv_logout_confirm);
        tv_title.setVisibility(View.VISIBLE);
        TextView tv_logout_cancel = (TextView) view.findViewById(R.id.tv_logout_cancel);
        tv_title.setText("温馨提示");
        tv_confirm.setText("保存");
        tv_logout_cancel.setText("不保存");
        tv_message.setText("是否保存草稿?");
        //确定
        tv_logout_confirm.setOnClickListener(v -> {
            if (isST) {
                stCheck();//商砼验证
            } else {
                goodsCheck();//商品验证
            }
            roundCornerDialog.dismiss();
        });

        //取消
        tv_logout_cancel.setOnClickListener(v -> {
            roundCornerDialog.dismiss();
            finish();
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(999 == resultCode) {
            publishPopViewModel.getBrandList(mDataBinding.llPar, mViewModel.goodsPublishEntity.get().cid3);
        } else if(998 == resultCode) {
            publishPopViewModel.getStorageList(mDataBinding.llPar, false);
        } else if (resultCode == RESULT_OK) {
            if (requestCode == PictureConfig.CHOOSE_REQUEST) {// 图片结果回调
                // 图片、视频、音频选择结果回调
                //imageUploadViewModel.imageAdapter.addData(imageUploadViewModel.imageAdapter.convertDataToAlbumEntity(PictureSelector.obtainMultipleResult(data), true));
            } else if (requestCode == PictureConfig.TYPE_VIDEO) {
//                locVideoList = PictureSelector.obtainMultipleResult(data);
//                videoSelectPopWindow.setData(locVideoList, indexVideo);
                videoUploadViewModel.setLocalMediaData(PictureSelector.obtainMultipleResult(data));
            } else if (requestCode == IMAGE_ADAPTER_LIST) {
                // ImageAdapter 数据设置
                //imageUploadViewModel.imageAdapter.addData(imageUploadViewModel.imageAdapter.convertDataToAlbumEntity(PictureSelector.obtainMultipleResult(data), false));
            } else if (requestCode == VIDEO_ADAPTER_LIST) {
                // VideoAdapter 数据设置
                mPVideoSelectList.put(dcpViewModel.mVideoAdapterPosition, PictureSelector.obtainMultipleResult(data));
                videoListSelectPopWindow.setData(PictureSelector.obtainMultipleResult(data), dcpViewModel.mPVideoIndex.get(dcpViewModel.mVideoAdapterPosition));
            } else if (requestCode == 90001) {
                mViewModel.listService = (List<GoodsServicesEntity>) data.getSerializableExtra("listService");
                if (isHasChecked(mViewModel.listService)) {
                    mDataBinding.tvChip.setContent("已填写");
                } else {
                    mDataBinding.tvChip.setContent("");
                }
                mDataBinding.tvChip.setLabelNameColor(ColorUtils.getColor(R.color.color_tab_one));
                List<GoodsPublishInfoEntity> serverInfoDTOS = new ArrayList<>();
                for (int m = 0; m < mViewModel.listService.size(); m++) {
                    for (int n = 0; n < mViewModel.listService.get(m).value.size(); n++) {
                        for (int l = 0; l < mViewModel.listService.get(m).value.get(n).value.size(); l++) {
                            GoodsPublishInfoEntity itemInfo = new GoodsPublishInfoEntity();
                            itemInfo.serviceType = mViewModel.listService.get(m).serviceType;
                            itemInfo.serverName = mViewModel.listService.get(m).value.get(n).value.get(l).serverName;
                            itemInfo.attributeId = mViewModel.listService.get(m).value.get(n).value.get(l).attributeId;
                            itemInfo.attributeName = mViewModel.listService.get(m).value.get(n).value.get(l).attributeName;
                            itemInfo.checked = mViewModel.listService.get(m).value.get(n).value.get(l).checked;
                            itemInfo.customed = mViewModel.listService.get(m).value.get(n).value.get(l).customed;
                            itemInfo.id = mViewModel.listService.get(m).value.get(n).value.get(l).id;
                            itemInfo.sort = mViewModel.listService.get(m).value.get(n).value.get(l).sort;
                            itemInfo.serverId = mViewModel.listService.get(m).value.get(n).serverId;
                            itemInfo.deleted = mViewModel.listService.get(m).value.get(n).value.get(l).deleted;
                            serverInfoDTOS.add(itemInfo);
                        }
                    }
                }
                mViewModel.sTPublishEntity.get().serverInfoDTOS = serverInfoDTOS;
                mViewModel.goodsPublishEntity.get().serverInfoDTOS = serverInfoDTOS;
            }
        }
    }


    private boolean isHasChecked(List<GoodsServicesEntity> listService) {
        List<Integer> tempData = new ArrayList<>();
        for (int x = 0; x < listService.size(); x++) {
            for (int y = 0; y < listService.get(x).value.size(); y++) {
                for (int z = 0; z < listService.get(x).value.get(y).value.size(); z++) {
                    int checked = listService.get(x).value.get(y).value.get(z).checked;
                    tempData.add(checked);
                }
            }
        }
        return tempData.contains(1);
    }


    @Override
    public void OnWholeItemClick(View view) {
        if(view.getId() == R.id.item_tv_arr) {
            if (id == null)
                publishPopViewModel.getGoodsCategory(mDataBinding.llPar);
        } else if(view.getId() == R.id.item_tv_unit) {
            publishPopViewModel.getUnitList(mDataBinding.llPar);
        } else if(view.getId() == R.id.item_tv_futures) {
            showFuturesPopWindow();
        } else if(view.getId() == R.id.item_tv_storage) {
            publishPopViewModel.getStorageList(mDataBinding.llPar, false);
        } else if(view.getId() == R.id.item_tv_brand) {
            if (mViewModel.goodsPublishEntity.get().cid3 == null) {
                MToast.showToast(GoodsPublishActivity.this, "请先选择商品分类");
                return;
            }
            publishPopViewModel.getBrandList(mDataBinding.llPar, mViewModel.goodsPublishEntity.get().cid3);
        } else if(view.getId() == R.id.item_tv_area) {
            if (id == null) {
                if (mViewModel.provinceListLD.getValue() == null) {
                    mViewModel.getAreaModel();
                } else {
                    showCityPop();
                }
            }
        } else if(view.getId() == R.id.item_tv_spec) {
            if (!"".equals(mDataBinding.itemTvSpec.getContent())) {
                // fixme: 跳转规格组合及价格
//                    GoodsSkuEvent goodsEvent = new GoodsSkuEvent();
//                    goodsEvent.setMessageTag(EVENT_SELECT_ARR);
//                    goodsEvent.setMessageData(mViewModel.goodsPublishEntity.get().getSkuOldList());
//                    goodsEvent.setObject(combination);
//                    EventBus.getDefault().postSticky(goodsEvent);
            }
            // fixme: 跳转规格组合及价格
            //GoodsSpecActivity.start(GoodsPublishActivity.this, mViewModel.goodsPublishEntity.get().cid3, id);
        } else if(view.getId() == R.id.tv_chip) {
            // fixme: 跳转服务配置
//                Intent intent = new Intent(GoodsPublishActivity.this, ChipActivity.class);
//                intent.putExtra("listService", (Serializable) listService);
//                intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
//                GoodsPublishActivity.this.startActivityForResult(intent, 90001);
        }
    }

    @Override
    public void onBackPressed() {
        if (JCVideoPlayer.backPress()) {
            return;
        }
        super.onBackPressed();
    }

    //商品验证
    private void goodsCheck() {
        setOtherData();
        mViewModel.goodsPublishEntity.get().attrList = mViewModel.attrChildEntityList;
        if (mViewModel.goodsPublishEntity.get().attrList != null) {
            for (int i = 0; i < mViewModel.goodsPublishEntity.get().attrList.size(); i++) {
                if (mViewModel.goodsPublishEntity.get().attrList.get(i).attrValue == null && mViewModel.goodsPublishEntity.get().attrList.get(i).isfill != null && 1 == mViewModel.goodsPublishEntity.get().attrList.get(i).isfill) {
                    arrName = mViewModel.goodsPublishEntity.get().attrList.get(i).attrKey;
                    isSelect = false;
                    break;
                }
                isSelect = true;
            }
        }
        if (TextUtils.isEmpty(mDataBinding.itemTvArr.getContent())) {
            MToast.showToast(GoodsPublishActivity.this, "请选择商品分类");
            return;
        } else if (TextUtils.isEmpty(mDataBinding.etName.getText().toString().trim())) {
            MToast.showToast(GoodsPublishActivity.this, "请输入商品名称");
            return;
        } else if (mDataBinding.etName.getText().toString().trim().length() > 32) {
            MToast.showToast(GoodsPublishActivity.this, "商品名称字数超出限制");
            return;
        } else if (TextUtils.isEmpty(mDataBinding.itemTvStorage.getContent())) {
            MToast.showToast(GoodsPublishActivity.this, "请选择仓储");
            return;
        } else if (TextUtils.isEmpty(mDataBinding.itemTvTax.getContent())) {
            MToast.showToast(GoodsPublishActivity.this, "请输入税率");
            return;
        } else if (!isSelect && (mViewModel.arrListLD.getValue() != null && mViewModel.arrListLD.getValue().size() != 0)) {
            MToast.showToast(GoodsPublishActivity.this, "请选择" + arrName);
            return;
        } else if (TextUtils.isEmpty(mDataBinding.itemTvUnit.getContent())) {
            MToast.showToast(GoodsPublishActivity.this, "请选择单位");
            return;
        } else if (mViewModel.getSkuListLD.getValue() != null && mViewModel.getSkuListLD.getValue().size() != 0 && mDataBinding.itemTvSpec.getContent().equals("")) {
            MToast.showToast(GoodsPublishActivity.this, "请配置规格属性");
            return;
        } else if (imageUploadViewModel.imageAdapter.getDataSize() == 0) {
            MToast.showToast(GoodsPublishActivity.this, "请选择图片");
            return;
        } else if (TextUtils.isEmpty(mDataBinding.itemBuyLow.getContent())) {
            MToast.showToast(GoodsPublishActivity.this, "请输入最低采购量");
            return;
        }
        mViewModel.goodsPublishEntity.get().lowestBuy = mDataBinding.itemBuyLow.getContent();
        if (mDataBinding.itemTvSpec.getContent().equals("")) {
            if (mViewModel.arrListLD.getValue() != null && mViewModel.arrListLD.getValue().size() != 0 && TextUtils.isEmpty(mDataBinding.itemTvPrice.getContent())) {
                MToast.showToast(GoodsPublishActivity.this, "请输入单价");
                return;
            }
        }
        if (islooks) {
            if (TextUtils.isEmpty(mDataBinding.itemTvPrice.getContent())) {
                MToast.showToast(GoodsPublishActivity.this, "请输入单价");
                return;
            }
            if (Double.valueOf(mDataBinding.itemTvPrice.getContent()) == 0) {
                MToast.showToast(GoodsPublishActivity.this, "请输入正确的单价");
                return;
            }
            if (Objects.requireNonNull(mViewModel.goodsPublishEntity.get()).skuList != null && mViewModel.goodsPublishEntity.get().skuList.size() == 1 && !TextUtils.isEmpty(mViewModel.goodsPublishEntity.get().skuList.get(0).money) && !TextUtils.isEmpty(mViewModel.goodsPublishEntity.get().skuList.get(0).vipMaxPrice)) {
                BigDecimal money = new BigDecimal(mDataBinding.itemTvPrice.getContent());
                BigDecimal vipMaxPrice = new BigDecimal(mViewModel.goodsPublishEntity.get().skuList.get(0).vipMaxPrice);
                if (money.compareTo(vipMaxPrice) == -1) {
                    //平销价小于会员价
                    MToast.showToast(GoodsPublishActivity.this, "平销价不能低于会员价");
                    return;
                }
            }
        }
        if (TextUtils.isEmpty(mViewModel.goodsPublishEntity.get().brandName)) {
            mDataBinding.etTitleLook.setText(mViewModel.goodsPublishEntity.get().name + "/" + mViewModel.goodsPublishEntity.get().cid3Name);
        } else {
            mDataBinding.etTitleLook.setText(mViewModel.goodsPublishEntity.get().name + "/" +
                    mViewModel.goodsPublishEntity.get().brandName + "/" + mViewModel.goodsPublishEntity.get().cid3Name);
        }
        if (mViewModel.getSkuListLD.getValue() != null && combination != null && mViewModel.getSkuListLD.getValue().size() != 0) {
            for (int i = 0; i < combination.size(); i++) {
                combination.get(i).category = cId3;
                combination.get(i).unit = mDataBinding.itemTvUnit.getContent();
            }
            mViewModel.goodsPublishEntity.get().skuList = combination;
        } else {
            List<GoodsPublishSkuListEntity> skuList = new ArrayList<>();
            GoodsPublishSkuListEntity goodsPublishSkuListEntity = new GoodsPublishSkuListEntity();
            goodsPublishSkuListEntity.category = cId3;
            goodsPublishSkuListEntity.money = mDataBinding.itemTvPrice.getContent();
            goodsPublishSkuListEntity.type = 0;
            if (mViewModel.goodsPublishEntity.get().skuOldList != null && mViewModel.goodsPublishEntity.get().skuOldList.size() != 0)
                goodsPublishSkuListEntity.id = mViewModel.goodsPublishEntity.get().skuOldList.get(0).id;
            skuList.add(goodsPublishSkuListEntity);
            mViewModel.goodsPublishEntity.get().skuList = skuList;
        }
        mViewModel.goodsPublishEntity.get().companyId = SharedPreferenceUtil.getInstance().getSaveStringData(SharedPreferenceUtil.REAL_ID, "");
        if (imageUploadViewModel.imageAdapter.getDataSize() != 0) {
            // 如果用户选择了文件，那么在onActivityForResult里对adapter.getFileList()进行了赋值，需要从第一张开始上传
            imageUploadViewModel.uploadImage(1);
        } else {
            //uploadImgSuccess(imageUploadViewModel.imageAdapter.getUrls());
        }

    }

    //商砼验证
    private void stCheck() {
        setOtherData();
        if (TextUtils.isEmpty(mDataBinding.etName.getText().toString().trim())) {
            MToast.showToast(GoodsPublishActivity.this, "请输入商品名称");
            return;
        } else if (mDataBinding.etName.getText().toString().trim().length() > 32) {
            MToast.showToast(GoodsPublishActivity.this, "商品名称字数超出限制");
            return;
        } else if (TextUtils.isEmpty(mDataBinding.itemTvStorage.getContent())) {
            MToast.showToast(GoodsPublishActivity.this, "请选择仓储");
            return;
        } else if (TextUtils.isEmpty(mDataBinding.itemTvTax.getContent())) {
            MToast.showToast(GoodsPublishActivity.this, "请输入税率");
            return;
        } else if (!strengthCheck()) {
            MToast.showToast(GoodsPublishActivity.this, "请填写强度等级");
            return;
        } else if (!strengthCheckZero()) {
            MToast.showToast(GoodsPublishActivity.this, "强度等级价格不能为0,请仔细检查");
            return;
        } else if (imageUploadViewModel.imageAdapter.getDataSize() == 0) {
            MToast.showToast(GoodsPublishActivity.this, "请选择图片");
            return;
        }
        //名称
        mViewModel.sTPublishEntity.get().title = mDataBinding.etName.getText().toString();
        //描述
        mViewModel.sTPublishEntity.get().itemDesc = mDataBinding.etMark.getText().toString();
        //税率
        mViewModel.sTPublishEntity.get().tax = mDataBinding.itemTvTax.getContent();
        //单位
        mViewModel.sTPublishEntity.get().unit = mDataBinding.itemTvStunit.getContent();
        //泵送
        mViewModel.sTPublishEntity.get().pumpPrice = mDataBinding.itemTvBsprice.getContent();
        //配置
        mViewModel.sTPublishEntity.get().gradation1Price = mDataBinding.itemTvLeavePrice.getContent();
        strengthCheckData();
        if (wjjCheck()) {
            wjjCheckData();
        }
        if (strength != null) {
            for (int i = 0; i < strength.size(); i++) {
                if ("strength".equals(strength.get(i).name)) {
                    STArrEntity stArrEntity1 = new STArrEntity();
                    stArrEntity1 = strength.get(i);
                    stArrEntity1.childList = strengthEntityList;
                    mViewModel.sTPublishEntity.get().strengthList = stArrEntity1;
                    mViewModel.sTPublishEntity.get().strengthList.childList = strengthEntityList;
                } else if ("admixture".equals(strength.get(i).name)) {
                    STArrEntity stArrEntity2 = new STArrEntity();
                    stArrEntity2 = strength.get(i);
                    stArrEntity2.childList = wjjList;
                    mViewModel.sTPublishEntity.get().admixtureList = stArrEntity2;
                }
            }
        }
        mViewModel.sTPublishEntity.get().companyId = SharedPreferenceUtil.getInstance().getSaveStringData(SharedPreferenceUtil.REAL_ID, "");
        if (imageUploadViewModel.imageAdapter.getDataSize() != 0) {
            imageUploadViewModel.uploadImage(1);
        } else {
            //uploadImgSuccess(imageUploadViewModel.imageAdapter.getUrls());
        }
    }

    //强度等级验证
    private boolean strengthCheck() {
        for (int i = 0; i < mViewModel.strengthGradeAdapter.getData().size(); i++) {
            if (mViewModel.strengthGradeAdapter.getData().get(i).price != null && !"".equals(mViewModel.strengthGradeAdapter.getData().get(i).price)) {
                return true;
            }
        }
        return false;
    }

    //强度等级验证
    private boolean strengthCheckZero() {
        for (int i = 0; i < mViewModel.strengthGradeAdapter.getData().size(); i++) {
            if (mViewModel.strengthGradeAdapter.getData().get(i).price != null && !"".equals(mViewModel.strengthGradeAdapter.getData().get(i).price)
                    && Double.valueOf(mViewModel.strengthGradeAdapter.getData().get(i).price) == 0) {
                return false;
            }
        }
        return true;
    }

    //外加剂
    private boolean wjjCheck() {
        wjjList = new ArrayList<>();
        for (int i = 0; i < mViewModel.admixtureAdapter.getData().size(); i++) {
            if (mViewModel.admixtureAdapter.getData().get(i).price != null && !"".equals(mViewModel.admixtureAdapter.getData().get(i).price)) {
                return true;
            }
        }
        return false;
    }

    //强度等级数据转换
    private void strengthCheckData() {
        if (strengthEntityList == null) {
            strengthEntityList = new ArrayList<>();
        }
        strengthEntityList.clear();
        for (int i = 0; i < mViewModel.strengthGradeAdapter.getData().size(); i++) {
            if (mViewModel.strengthGradeAdapter.getData().get(i).price != null && !"".equals(mViewModel.strengthGradeAdapter.getData().get(i).price)) {
                strengthEntityList.add(mViewModel.strengthGradeAdapter.getData().get(i));
            }
        }
    }

    //外加剂数据转换
    private void wjjCheckData() {
        wjjList.clear();
        for (int i = 0; i < mViewModel.admixtureAdapter.getData().size(); i++) {
            if (mViewModel.admixtureAdapter.getData().get(i).price != null && !"".equals(mViewModel.admixtureAdapter.getData().get(i).price)) {
                wjjList.add(mViewModel.admixtureAdapter.getData().get(i));
            }
        }
    }

    /** 拿到动态控件的数据 */
    private void setOtherData() {
        if (isST) {
            mViewModel.sTPublishEntity.get().otherInfos = dcpViewModel.getOtherData();
        } else {
            mViewModel.goodsPublishEntity.get().otherInfos = dcpViewModel.getOtherData();
        }
    }

    public void MoveToPosition(LinearLayoutManager manager, int n) {
        manager.scrollToPositionWithOffset(n, 0);
        manager.setStackFromEnd(true);
    }

    public void setHtml(String htmls) {
        if (htmls == null) {
            mDataBinding.yesData.setVisibility(View.GONE);
            mDataBinding.yesDataSubtitle.setText("(暂无数据，手机不支持编辑，请使用电脑后台编辑)");
            return;
        }
        mDataBinding.yesData.setVisibility(View.VISIBLE);
        mDataBinding.yesDataSubtitle.setText("(手机不支持编辑，请使用电脑后台编辑)");
        Document doc = Jsoup.parseBodyFragment(htmls);
        Elements elements = doc.select("img");
        for (Element each : elements) {
            each.attr("style", "width:100%");
        }

        String html = doc.html();
        Document htmlResult = Jsoup.parseBodyFragment(html);
        elements = htmlResult.select("body");
        for (Element each : elements) {
            each.attr("style", "font-size:34px");
        }
        elements = htmlResult.getElementsByTag("p");
        for (Element element : elements) {
            element.attr("style", "color:#666666");
        }
        MLog.d("", htmlResult.html());

        mDataBinding.webView.loadData(htmlResult.html(), "text/html; charset=UTF-8", null);
        mDataBinding.webView.getSettings().setJavaScriptEnabled(true);
        mDataBinding.webView.getSettings().setSupportZoom(false);
        mDataBinding.webView.getSettings().setBuiltInZoomControls(false);
        mDataBinding.webView.getSettings().setUseWideViewPort(true);
        mDataBinding.webView.getSettings().setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NARROW_COLUMNS);
        mDataBinding.webView.getSettings().setLoadWithOverviewMode(true);
        mDataBinding.webView.setBackgroundColor(getResources().getColor(R.color.color_F5F5F5)); // 设置背景色
        mDataBinding.webView.setWebViewClient(new WebViewClient() {
            @Override
            public void onPageFinished(WebView view, String url) {
                mDataBinding.webView.loadUrl("javascript:document.body.style.padding=\"3.2%\"; void 0");
            }
        });
    }
}